<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveSupport::CoreExtensions::Array::Conversions</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveSupport::CoreExtensions::Array::Conversions</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../../files/vendor/rails/activesupport/lib/active_support/core_ext/array/conversions_rb.html">
                vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000632">to_formatted_s</a>&nbsp;&nbsp;
      <a href="#M000630">to_param</a>&nbsp;&nbsp;
      <a href="#M000631">to_query</a>&nbsp;&nbsp;
      <a href="#M000629">to_sentence</a>&nbsp;&nbsp;
      <a href="#M000633">to_xml</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000632" class="method-detail">
        <a name="M000632"></a>

        <div class="method-heading">
          <a href="#M000632" class="method-signature">
          <span class="method-name">to_formatted_s</span><span class="method-args">(format = :default)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000632-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000632-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 48</span>
48:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_formatted_s</span>(<span class="ruby-identifier">format</span> = <span class="ruby-identifier">:default</span>)
49:           <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">format</span>
50:             <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:db</span>
51:               <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:empty?</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">empty?</span>
52:                 <span class="ruby-value str">&quot;null&quot;</span>
53:               <span class="ruby-keyword kw">else</span>
54:                 <span class="ruby-identifier">collect</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span>.<span class="ruby-identifier">id</span> }.<span class="ruby-identifier">join</span>(<span class="ruby-value str">&quot;,&quot;</span>)
55:               <span class="ruby-keyword kw">end</span>
56:             <span class="ruby-keyword kw">else</span>
57:               <span class="ruby-identifier">to_default_s</span>
58:           <span class="ruby-keyword kw">end</span>
59:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000630" class="method-detail">
        <a name="M000630"></a>

        <div class="method-heading">
          <a href="#M000630" class="method-signature">
          <span class="method-name">to_param</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Calls <a href="Conversions.html#M000630">to_param</a> on all its elements
and joins the result with slashes. This is used by url_for in Action Pack.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000630-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000630-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 28</span>
28:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_param</span>
29:           <span class="ruby-identifier">map</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">:to_param</span>).<span class="ruby-identifier">join</span> <span class="ruby-value str">'/'</span>
30:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000631" class="method-detail">
        <a name="M000631"></a>

        <div class="method-heading">
          <a href="#M000631" class="method-signature">
          <span class="method-name">to_query</span><span class="method-args">(key)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Converts an array into a string suitable for use as a URL query string,
using the given <tt>key</tt> as the param name.
</p>
<h4>Example:</h4>
<pre>
  ['Rails', 'coding'].to_query('hobbies') =&gt; &quot;hobbies%5B%5D=Rails&amp;hobbies%5B%5D=coding&quot;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000631-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000631-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 37</span>
37:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_query</span>(<span class="ruby-identifier">key</span>)
38:           <span class="ruby-identifier">collect</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">to_query</span>(<span class="ruby-node">&quot;#{key}[]&quot;</span>) } <span class="ruby-operator">*</span> <span class="ruby-value str">'&amp;'</span>
39:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000629" class="method-detail">
        <a name="M000629"></a>

        <div class="method-heading">
          <a href="#M000629" class="method-signature">
          <span class="method-name">to_sentence</span><span class="method-args">(options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Converts the array to a comma-separated sentence where the last element is
joined by the connector word. Options:
</p>
<ul>
<li><tt>:connector</tt> - The word used to join the last element in arrays with
two or more elements (default: &quot;and&quot;)

</li>
<li><tt>:skip_last_comma</tt> - Set to true to return &quot;a, b and c&quot;
instead of &quot;a, b, and c&quot;.

</li>
</ul>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000629-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000629-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 10</span>
10:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_sentence</span>(<span class="ruby-identifier">options</span> = {})
11:           <span class="ruby-identifier">options</span>.<span class="ruby-identifier">assert_valid_keys</span>(<span class="ruby-identifier">:connector</span>, <span class="ruby-identifier">:skip_last_comma</span>)
12:           <span class="ruby-identifier">options</span>.<span class="ruby-identifier">reverse_merge!</span> <span class="ruby-identifier">:connector</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">'and'</span>, <span class="ruby-identifier">:skip_last_comma</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">false</span>
13:           <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:connector</span>] = <span class="ruby-node">&quot;#{options[:connector]} &quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:connector</span>].<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:connector</span>].<span class="ruby-identifier">strip</span> <span class="ruby-operator">==</span> <span class="ruby-value str">''</span>
14: 
15:           <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">length</span>
16:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">0</span>
17:               <span class="ruby-value str">&quot;&quot;</span>
18:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">1</span>
19:               <span class="ruby-keyword kw">self</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">to_s</span>
20:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">2</span>
21:               <span class="ruby-node">&quot;#{self[0]} #{options[:connector]}#{self[1]}&quot;</span>
22:             <span class="ruby-keyword kw">else</span>
23:               <span class="ruby-node">&quot;#{self[0...-1].join(', ')}#{options[:skip_last_comma] ? '' : ','} #{options[:connector]}#{self[-1]}&quot;</span>
24:           <span class="ruby-keyword kw">end</span>
25:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000633" class="method-detail">
        <a name="M000633"></a>

        <div class="method-heading">
          <a href="#M000633" class="method-signature">
          <span class="method-name">to_xml</span><span class="method-args">(options = {}) {|xml if block_given?| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000633-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000633-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 61</span>
61:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_xml</span>(<span class="ruby-identifier">options</span> = {})
62:           <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Not all elements respond to to_xml&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">all?</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-identifier">:to_xml</span> }
63: 
64:           <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:root</span>]     <span class="ruby-operator">||=</span> <span class="ruby-identifier">all?</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-identifier">first</span>.<span class="ruby-identifier">class</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">first</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">!=</span> <span class="ruby-value str">&quot;Hash&quot;</span> } <span class="ruby-operator">?</span> <span class="ruby-identifier">first</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">underscore</span>.<span class="ruby-identifier">pluralize</span> <span class="ruby-operator">:</span> <span class="ruby-value str">&quot;records&quot;</span>
65:           <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:children</span>] <span class="ruby-operator">||=</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:root</span>].<span class="ruby-identifier">singularize</span>
66:           <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:indent</span>]   <span class="ruby-operator">||=</span> <span class="ruby-value">2</span>
67:           <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:builder</span>]  <span class="ruby-operator">||=</span> <span class="ruby-constant">Builder</span><span class="ruby-operator">::</span><span class="ruby-constant">XmlMarkup</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">:indent</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:indent</span>])
68: 
69:           <span class="ruby-identifier">root</span>     = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:root</span>).<span class="ruby-identifier">to_s</span>
70:           <span class="ruby-identifier">children</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:children</span>)
71: 
72:           <span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-identifier">options</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:dasherize</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:dasherize</span>]
73:             <span class="ruby-identifier">root</span> = <span class="ruby-identifier">root</span>.<span class="ruby-identifier">dasherize</span>
74:           <span class="ruby-keyword kw">end</span>
75: 
76:           <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:builder</span>].<span class="ruby-identifier">instruct!</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:skip_instruct</span>)
77: 
78:           <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">merge</span>({ <span class="ruby-identifier">:root</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">children</span> })
79: 
80:           <span class="ruby-identifier">xml</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:builder</span>]
81:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">empty?</span>
82:             <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">tag!</span>(<span class="ruby-identifier">root</span>, <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:skip_types</span>] <span class="ruby-operator">?</span> {} <span class="ruby-operator">:</span> {<span class="ruby-identifier">:type</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">&quot;array&quot;</span>})
83:           <span class="ruby-keyword kw">else</span>
84:             <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">tag!</span>(<span class="ruby-identifier">root</span>, <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:skip_types</span>] <span class="ruby-operator">?</span> {} <span class="ruby-operator">:</span> {<span class="ruby-identifier">:type</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">&quot;array&quot;</span>}) {
85:               <span class="ruby-keyword kw">yield</span> <span class="ruby-identifier">xml</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block_given?</span>
86:               <span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_xml</span>(<span class="ruby-identifier">opts</span>.<span class="ruby-identifier">merge!</span>({ <span class="ruby-identifier">:skip_instruct</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span> })) }
87:             }
88:           <span class="ruby-keyword kw">end</span>
89:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>